import axios from 'axios';

axios.defaults.timeout = 5000;
axios.defaults.baseURL = '/api/';

//http request 拦截器
axios.interceptors.request.use(
    config => {
        // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
        config.data = JSON.stringify(config.data);
        config.headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        // if(token){
        //   config.params = {'token':token}
        // }
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
);

//http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code != null) {
            switch (response.data.code) {
                case -1:
                    // 店铺未授权
                    router.replace({
                        path: '/login',
                        query: {
                            redirect: router.currentRoute.fullPath,
                        }
                        //登录成功后跳入浏览的当前页面
                    });
                    break;
                default:

            }
        } else {
            console.log('不正常的请求: 没有返回码', response)

            throw new Error('不正常的请求: 没有返回码')
        }

        return response;
    },
    (err) => {
        // 这里是返回状态码不为200时候的错误处理
        if (err && err.response) {
            console.log(err.response)
            switch (err.response.status) {
                case 400:
                    err.message = '请求错误'
                    break

                case 401:
                    err.message = '未授权，请登录'
                    break

                case 403:
                    err.message = '拒绝访问'
                    break

                case 404:
                    err.message = '请求地址出错'
                    break

                case 422:
                    let message422 = ''
                    if (err.response.data.errors != undefined) {
                        let errRes = err.response.data.errors
                        for (var key in errRes) {
                            let reson = ''
                            let count = errRes[key].length
                            for (let i=0; i<count; i++) {
                                reson += errRes[key][i]
                            }
                            message422 += key+": "+ reson +"; "
                        }
                    } else {
                        message422 = '表单验证失败'
                    }
                    err.message = message422
                    break

                case 408:
                    err.message = '请求超时'
                    break

                case 500:
                    err.message = '服务器内部错误'
                    break

                case 501:
                    err.message = '服务未实现'
                    break

                case 502:
                    err.message = '网关错误'
                    break

                case 503:
                    err.message = '服务不可用'
                    break

                case 504:
                    err.message = '网关超时'
                    break

                case 505:
                    err.message = 'HTTP版本不受支持'
                    break

                default:
            }
        }
        return Promise.reject(err)
    }
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
let Http = {
    get(url, params = {}) {
        return new Promise((resolve, reject) => {
            axios.get(url, {
                params: params
            })
                .then(response => {
                    resolve(response.data);
                })
                .catch(err => {
                    reject(err)
                })
        })
    },


    /**
     * 封装post请求
     * @param url
     * @param data
     * @returns {Promise}
     */

    post(url, data = {}) {
        return new Promise((resolve, reject) => {
            axios.post(url, data)
                .then(response => {
                    resolve(response.data);
                }, err => {
                    reject(err)
                })
        })
    },

    /**
     * 封装patch请求
     * @param url
     * @param data
     * @returns {Promise}
     */

    patch(url, data = {}) {
        return new Promise((resolve, reject) => {
            axios.patch(url, data)
                .then(response => {
                    resolve(response.data);
                }, err => {
                    reject(err)
                })
        })
    },

    /**
     * 封装put请求
     * @param url
     * @param data
     * @returns {Promise}
     */

    put(url, data = {}) {
        return new Promise((resolve, reject) => {
            axios.put(url, data)
                .then(response => {
                    resolve(response.data);
                }, err => {
                    reject(err)
                })
        })
    }
}

export default Http;